﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Util.Log;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 空间参考相关操作类
    /// </summary>
    public static class SpatialReferenceHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="initialDirectory"></param>
        /// <param name="spname"></param>
        /// <returns></returns>
        public static ISpatialReference GetSR(string initialDirectory = "", string spname = "")
        {
            ISpatialReference pSpatialRef = GetSpatialReferenceFromPrj(initialDirectory);
            if (pSpatialRef == null)
            {
                LogHelper.WriteErrLog("空间参考打开失败！");
                return null;
            }

            SetSpatialDomain(pSpatialRef, spname);

            //还差选择Z坐标
            IVerticalCoordinateSystem pVerticalCoordinateSystem = GetVerticalCoordinateSystem(initialDirectory);
            if (pVerticalCoordinateSystem != null)
            {
                (pSpatialRef as ISpatialReference3).VerticalCoordinateSystem = pVerticalCoordinateSystem;
            }


            return pSpatialRef;
        }

        private static IVerticalCoordinateSystem GetVerticalCoordinateSystem(string initialDirectory = "")
        {
            OpenFileDialog openFileDialog1;
            openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Title = "请选择Z空间参考文件";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = "空间参考文件(*.prj)|*.prj";

            //added by sheng 让用户每次打开的时候，直接定位到保存坐标文件的目录
            openFileDialog1.InitialDirectory = initialDirectory;// AppConfig.CoordinatePath;
            openFileDialog1.FileName = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                StreamReader sr = File.OpenText(openFileDialog1.FileName);
                string srInfo = sr.ReadToEnd();
                ISpatialReferenceFactory3 pSpatRefFact = new SpatialReferenceEnvironmentClass();

                IVerticalCoordinateSystem pVerticalCoordinateSystem = pSpatRefFact.CreateVerticalCoordinateSystemFromESRISpatialReference(srInfo);
                return pVerticalCoordinateSystem;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取空间参考
        /// </summary>
        /// <returns></returns>
        private static ISpatialReference GetSpatialReferenceFromPrj(string initialDirectory = "")
        {
            OpenFileDialog openFileDialog1;
            openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Title = "请选择XY空间参考文件";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = "空间参考文件(*.prj)|*.prj";

            //added by sheng 让用户每次打开的时候，直接定位到保存坐标文件的目录
            openFileDialog1.InitialDirectory = initialDirectory;// AppConfig.CoordinatePath;
            openFileDialog1.FileName = "";

            ISpatialReference pSpatRef;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.FileName == "") return null;
                ISpatialReferenceFactory3 pSpatRefFact = new SpatialReferenceEnvironmentClass();

                pSpatRef =
                    pSpatRefFact.CreateESRISpatialReferenceFromPRJFile(openFileDialog1.FileName);

                if (pSpatRef == null)
                {
                    LogHelper.WriteErrLog("没有找到投影信息或者投影文件已经破坏，请检查！");
                    return null;
                }
                return pSpatRef;
            }
            else
            {
                pSpatRef = new UnknownCoordinateSystemClass();
            }
            return pSpatRef;
        }

        /// <summary>
        /// 设置范围
        /// </summary>
        /// <param name="pSpatialRef"></param>
        /// <param name="spname"></param>
        private static void SetSpatialDomain(ISpatialReference pSpatialRef,string spname = "")
        {
            FrmSetSpatialRef ssr = new FrmSetSpatialRef();
            if (!string.IsNullOrWhiteSpace(spname))
                ssr.SpatialFileName = spname;
            if (ssr.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pSpatialRef.SetDomain(ssr.Minx, ssr.Maxx, ssr.Miny, ssr.Maxy);
            }
            else
            {
                pSpatialRef.SetDomain(-5123200, 900714802274.099, -10002100, 900709923374.099);
            }
            pSpatialRef.SetMDomain(-100000, 900719825474.099);
            pSpatialRef.SetZDomain(-100000, 900719825474.099);
        }
    }
}
